﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Sync_IniControler
{
    class IniControler
    {
        #region Enum

        private enum TypeDataIni
        {
            COMMENT,
            SECTION,
            KEY_VALUE,
            OTHER
        };

        #endregion

        #region Attributes

        private StreamReader _sr;
        private string _fileName;
        private Dictionary<string, TypeDataIni> _regex;
        private List<IniSection> _sections;
        private int _currentSection;

        #endregion

        #region Methods

        public IniControler()
        {
            _fileName = "";
            InitRegex();
            InitSection();
        }

        public IniControler(string fileName)
        {
            _fileName = fileName;
            InitRegex();
            InitSection();
            this.Load();
        }

        private void InitSection()
        {
            _sections = new List<IniSection>();
            _sections.Add(new IniSection());
            _sections[0].Section = "noName";
           // NewSection();
        }

        private void InitRegex()
        {
            _regex = new Dictionary<string, TypeDataIni>();

            //Comments
            _regex.Add("^(.)*\\#", TypeDataIni.COMMENT);
            _regex.Add("^(.)*;", TypeDataIni.COMMENT);


            //Section
            _regex.Add("^(.)*\\[(.)*]$", TypeDataIni.SECTION);

            //Value
            _regex.Add("^(.)*\\=(.)*$", TypeDataIni.KEY_VALUE);

        }

        public void Load(string fileName)
        {
            _fileName = fileName;
            this.Load();
        }

        private void Load()
        {
            try
            {
                _sr = new StreamReader(_fileName);

                string buffer;
                bool global = true;

                
                while ((buffer = _sr.ReadLine()) != null)
                {
                    bool find = false;
                 
                    foreach (var tPair in _regex)
                    {
                        if (find)
                            break;

                        Match match = Regex.Match(buffer, tPair.Key,
                                        RegexOptions.IgnoreCase);

                        if (match.Success)
                        {
                            switch (tPair.Value)
                            {
                                case TypeDataIni.COMMENT:
                                    Debug.Write("COMMENTAIRE ! -> ");

                                    if (global)
                                        this.SectionNoName.Comments.Add(buffer);
                                    else
                                        this.CurrentSection.Comments.Add(buffer);

                                    find = true;
                                    break;

                                case TypeDataIni.SECTION:
                                    Debug.Write("SECTION ! -> ");

                                    NewSection();
                                    this.CurrentSection.Section = buffer;

                                    global = false;
                                    find = true;
                                    break;

                                case TypeDataIni.KEY_VALUE:
                                    Debug.Write("KEY_VALUE ! -> ");

                                    

                                    find = true;
                                    break;

                                default: Debug.Write("Unknow type line");
                                    break;
                            }
                        }
                    }
                    Debug.WriteLine(buffer);
                }
                _sr.Close();
            }
            catch
            {
                //Do anything ! I don't give a shit...
            }
        }

        public void NewSection()
        {
            _sections.Add(new IniSection());
            _currentSection++;
        }

        #endregion

        #region Get & Set

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        public List<IniSection> Section
        {
            get { return _sections; }
            set { _sections = value; }
        }

        public IniSection SectionNoName
        {
            get { return this.Section[0]; }
            set { this.Section[0] = value; }
        }

        public IniSection CurrentSection
        {
            get { return _sections[_currentSection]; }
            set { _sections[_currentSection] = value; }
        }

        #endregion

    }
}
